/*
** send_signals.c for  in /u/all/caille_c/cu/rendu/c/minitalk/client
** 
** Made by carole cailleux
** Login   <caille_c@epitech.net>
** 
** Started on  Thu Mar 11 12:12:41 2010 carole cailleux
** Last update Wed Mar 17 19:24:21 2010 carole cailleux
*/

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include "client.h"

extern int	gl_sig_ok;

void		dec_to_bin(int chara, int pid)
{
  int		i;
  int		characpy;

  i = 0;
  characpy = chara;
  while (i < 8)
    {
      send_sign(chara & 1, pid);
      chara = chara >> 1;
      i++;
      if (characpy == 0 && i == 8)
	{
	  usleep(10);
	  exit(1);
	}
      while (!gl_sig_ok)
	usleep(20);
      gl_sig_ok = 0;
    }
}

void		send_sign(int nb, int pid)
{
  if (nb == 0)
    kill(pid, SIGUSR1);
  else if (nb == 1)
    kill(pid, SIGUSR2);
}

void		pid_to_bin(int pid_cl, int pid_srv)
{
  int		i;

  i = 0;
  while (i < 32)
    {
      send_sign(pid_cl % 2, pid_srv);
      pid_cl = pid_cl / 2;
      i++;
      usleep(1000);
    }
}

void		send_pid(int pid)
{
  int		pid_client;

  pid_client = getpid();
  my_putstr("(Pid client = ");
  my_put_nbr(pid_client);
  my_putstr(")\n");
  pid_to_bin(pid_client, pid);
}
